package com.ymt.bpm.web.action.engine;

import com.ymt.bpm.appboot.BpmpProperties;
import com.ymt.bpm.util.Const;
import com.ymt.bpm.util.ModuleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Johnny on 2017/3/24.
 */
@Controller
@RequestMapping("/view/process")
@EnableConfigurationProperties(BpmpProperties.class)
public class DesignerController {

    @Autowired
    Environment env;

    @RequestMapping(path="/design/{bpdId}")
    public ModelAndView design(HttpServletRequest request,
                               @PathVariable("bpdId")String bpdId,
                               @RequestParam(value = "versionid",
                                       defaultValue = "",
                                       required = false)String versionid) {
        ModelAndView view = getDesignerView(request);
        view.addObject("isEditor", "true");
        view.addObject("flowid", bpdId);
        view.addObject("versionid", versionid);
        return view;
    }

    @RequestMapping(path="/view/{bpdId}")
    public ModelAndView view(HttpServletRequest request,
                             @PathVariable("bpdId")String bpdId,
                             @RequestParam(value = "versionid",
                                     defaultValue = "",
                                     required = false)String versionid) {
        ModelAndView view = getDesignerView(request);
        view.addObject("isEditor", "false");
        view.addObject("flowid", bpdId);
        view.addObject("versionid", versionid);
        return view;
    }

    @RequestMapping(path="/pichange/{piEid}")
    public ModelAndView pichange(HttpServletRequest request,
                             @PathVariable("piEid")String piEid) {
        ModelAndView view = getDesignerView(request);
        view.setViewName("process/designer/designer_pichange");
        view.addObject("isEditor", "true");
        view.addObject("flowid", "");
        view.addObject("versionid", "");
        view.addObject("piEid", piEid);
        return view;
    }

    private ModelAndView getDesignerView(HttpServletRequest request) {
        Map<String, String> userDetail = (Map<String, String>) SecurityContextHolder.getContext().getAuthentication().getDetails();
        String lang = userDetail.get(Const.LANG);
        String loginName = userDetail.get(Const.LOGINNAME);
        String displayName = userDetail.get(Const.DISPLAYNAME);
        String modulePath = "/static/jsplatform/admin/designer/modules";
        String[] profile = env.getActiveProfiles();
        String cdn = env.getProperty("bpmp.cdn", "/bpmp-resources");

        ModelAndView view = new ModelAndView("process/designer/designer");
        view.addObject("profile", profile[0]);
        view.addObject("cdn", cdn);
        view.addObject(Const.LANG, lang);
        view.addObject(Const.LOGINNAME, loginName);
        view.addObject(Const.DISPLAYNAME, displayName);
        view.addObject("fmdCtx", env.getProperty("fmd.ctx"));

        view.addObject("isEditor", "true");

        if ("dev".equalsIgnoreCase(profile[0])) {
            String realPath = env.getProperty("bpmp.resource-realpath");
            view.addObject("modulesPath", modulePath);
            view.addObject("activityModules", ModuleUtil.getModuleNames(realPath, modulePath + "/activity"));
            view.addObject("eventModules", ModuleUtil.getModuleNames(realPath, modulePath + "/event"));
            view.addObject("gatewayModules", ModuleUtil.getModuleNames(realPath, modulePath + "/gateway"));
            view.addObject("layoutModules", ModuleUtil.getModuleNames(realPath, modulePath + "/layout"));
            view.addObject("transitionModules", ModuleUtil.getModuleNames(realPath, modulePath + "/transition"));
            view.addObject("miscModules", ModuleUtil.getModuleNames(realPath, modulePath + "/misc"));
        } else {
            view.addObject("modulesPath", "");
            view.addObject("activityModules", "''");
            view.addObject("eventModules", "''");
            view.addObject("gatewayModules", "''");
            view.addObject("layoutModules", "''");
            view.addObject("transitionModules", "''");
            view.addObject("miscModules", "''");
        }

        return view;
    }

}
